<div>
    @if (config()['app']['locale'] === 'en')
        <button class="switch-lang-btn btn btn-primary btn-mini btn-outline" data-language="ZH_CN">
            简体中文
        </button>
    @elseif (config()['app']['locale'] === 'ZH_CN')
        <button class="switch-lang-btn btn btn-primary btn-mini btn-outline" data-language="en">
            English
        </button>
    @endif
    <input type="hidden" name="token" id="token" value="{{csrf_token()}}">
</div>

<script>
    // dcat-admin内部使用的jQuery
    // Dcat.ready中的回调会在所有js脚本加载完成之后执行
    Dcat.ready(function () {
        $('.switch-lang-btn').on('click', function () {
            let language = $(this).attr('data-language');
            let token = $('#token').val()
            $.ajax({
                url: '{{ url('admin/setLanguage') }}',
                type: 'post',
                data: {
                    'language': language,
                    '_token': token
                },
                success: function (data) {
                    if (data.code === 200) {
                        window.location.reload()
                    } else {
                        alert(data.message)
                    }
                }
            })
        })
    })
</script>
